//
// Created by Administrator on 2022/10/22.
//

//使用链式结构实现队列
//要使用一个队头指针和一个队尾指针
#include <stdio.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
typedef int ElementType;
typedef int Status;
//定义队列节点
typedef struct LQNOde{
    ElementType data;
    struct LQNOde* next;
}LQNOde,*LQNOdePtr;
//定义队头指针和队尾指针
typedef struct{
    LQNOdePtr front,rear;
}LinkQueue;

void initQueue(LinkQueue* queue){
    queue->front = (LQNOdePtr)malloc(sizeof(struct LQNOde));
    queue->rear=queue->front;
}
//入队操作
Status EnQueue(LinkQueue* queue,ElementType e){
    LQNOdePtr p = (LQNOdePtr)malloc(sizeof(struct LQNOde));
    p->data = e;
    p->next = NULL;
    queue->rear->next = p;
    queue->rear = p;
    return OK;
}

//出队操作
Status DeQueue(LinkQueue* queue,ElementType* e){
    if(queue->rear==queue->front){
        return ERROR;
    }
    LQNOdePtr p = queue->front->next;
    *e = p->data;
    queue->front->next = p->next;
    if(queue->rear==p){ //如果队列清空,将队尾等于头节点
        queue->rear=queue->front;
    }
    free(p);
}

void showQueue(LinkQueue queue){
    LQNOdePtr p = queue.front->next;
    while(p){
        printf("%d\n",p->data);
        p=p->next;
    }
}
void main(){
    LinkQueue queue;
    initQueue(&queue);
    ElementType tmp;
    for (int i = 0; i < 6; ++i) {
        EnQueue(&queue,i);
    }
    printf("de:\n");
    for (int i = 0; i < 3; ++i) {
        DeQueue(&queue,&tmp);
        printf("%d\n",tmp);
    }
    printf("show:\n");
    showQueue(queue);
}

